package com.zlq.cn;

import com.alibaba.fastjson.util.IOUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BuildAndUpload {
    private static final String LINUX = "/bin/sh -c ";
    private static final String WINDOWS = "cmd.exe /c ";
    private static final Runtime RUNTIME = Runtime.getRuntime();
    private static final String IP = "8.129.183.140";
    private static final String USER = "root";
    private static final String PSWD = "ll-2020-qq";
    private static final SSHAgent SSH_AGENT = new SSHAgent();

    private static final String SERIVCE_DIR = "/data/zlq/project/test/apphotel/";
    private static final String JAR_NAME = "zlq-0.0.1-SNAPSHOT.jar";

    static {
        SSH_AGENT.initSession(IP, USER, PSWD);
    }

    public static void main(String[] args) throws Exception {
        String projectDir = System.getProperty("user.dir");
        System.out.println(projectDir);
//      printlnWindowsExec(" mvnw clean package ");
        System.out.println("开始执行编译");
        printlnWindowsExec(" cd /d %s", projectDir);
        printlnWindowsExec(" gradlew bootJar ");
        System.out.println("编译完成,修改服务器久文件名(备份)");
        String newName = FilenameUtils.getBaseName(JAR_NAME) + new SimpleDateFormat("-yyyy-MM-dd-HH-mm-ss.").format(new Date()) + FilenameUtils.getExtension(JAR_NAME);
        System.out.println(SSH_AGENT.execCommand("cd %s; mv %s %s", SERIVCE_DIR, JAR_NAME, newName));
        System.out.println("修改完毕,准备上传文件");
        String localFile = String.format("%s\\build\\libs\\%s", projectDir, JAR_NAME);
        System.out.println(localFile);
        SSH_AGENT.transferFile(localFile, SERIVCE_DIR);
        System.out.println("文件上传成功,执行重启脚本");
        System.out.println(SSH_AGENT.execCommand("cd %s; ./wss.sh restart", SERIVCE_DIR));
        System.out.println("执行完毕,部署结束");
        SSH_AGENT.close();
    }

    private static void printlnWindowsExec(String exec, Object... args) {
        printlnExec(WINDOWS, String.format(exec, args));
    }

    private static void printlnLinuxExec(String exec, Object... args) {
        printlnExec(LINUX, String.format(exec, args));
    }

    private static void printlnExec(String platform, String exec) {
        try {
            printlnInputStream(RUNTIME.exec(platform + exec).getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printlnInputStream(InputStream inputStream) {
        BufferedReader bufferedReader = null;
        try {
            String line;
            StringBuilder sb = new StringBuilder();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            System.out.println(sb);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.close(bufferedReader);
        }
    }
}
